package com.example.antdback.utils;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * 分布式锁实现
 *
 * @author lijianguo
 * @since 2019-01-12 15:55:57
 */
@Component
public class RedisLockerUtil {


	/* RedissonClient已经由配置类生成，这里自动装配即可 */
	@Resource
	private RedissonClient redissonClient;

	//lock(), 拿不到lock就不罢休，不然线程就一直block
	public RLock lock(String lockKey) {
		RLock lock = redissonClient.getLock(lockKey);
		lock.lock();
		return lock;
	}

	//leaseTime为加锁时间，单位为秒
	public RLock lock(String lockKey, long leaseTime) {
		RLock lock = redissonClient.getLock(lockKey);
		lock.lock(leaseTime, TimeUnit.SECONDS);
		return lock;
	}

	//timeout为加锁时间，时间单位由unit确定
	public RLock lock(String lockKey, TimeUnit unit ,long timeout) {
		RLock lock = redissonClient.getLock(lockKey);
		lock.lock(timeout, unit);
		return lock;
	}
	//tryLock()，马上返回，拿到lock就返回true，不然返回false。
	//带时间限制的tryLock()，拿不到lock，就等一段时间，超时返回false.
	public boolean tryLock(String lockKey, TimeUnit unit, long waitTime, long leaseTime) {
		RLock lock = redissonClient.getLock(lockKey);
		try {
			return lock.tryLock(waitTime, leaseTime, unit);
		} catch (InterruptedException e) {
			return false;
		}
	}

	public void unlock(String lockKey) {
		RLock lock = redissonClient.getLock(lockKey);
		if(lock != null) {
			lock.unlock();
		}
	}

	public void unlock(RLock lock) {
		lock.unlock();
	}
}
